package com.sh.rbac.controller;

import cn.hutool.json.JSONUtil;
import com.sh.rbac.core.common.result.Result;
import com.sh.rbac.core.encrypt.annotation.ApiEncrypt;
import com.sh.rbac.domain.dto.auth.AccountLoginDTO;
import com.sh.rbac.domain.entity.User;
import com.sh.rbac.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 登录认证控制器
 *
 * @author lucky
 * @since 2024-04-07
 */

@Tag(name = "登录认证", description = "登录认证相关操作")
@RequestMapping("/auth")
@RestController
public class AuthController {

    @Autowired
    private AuthService authService;

    @ApiEncrypt
    @Operation(summary = "登录", description = "用户登录")
    @ApiResponse(responseCode = "200", description = "登录成功")
    @PostMapping("login")
    public Result<?> login(@RequestBody String body) {
        AccountLoginDTO accountLoginDTO = JSONUtil.toBean(body, AccountLoginDTO.class);
        return Result.success(authService.accountLogin(accountLoginDTO));
    }

    @Operation(summary = "获取用户信息", description = "获取用户信息")
    @ApiResponse(responseCode = "200", description = "获取成功")
    @GetMapping("info")
    public Result<User> getInfo() {
        return Result.success(authService.getInfo());
    }

    @Operation(summary = "退出登录", description = "用户退出登录")
    @ApiResponse(responseCode = "200", description = "成功退出登录")
    @PostMapping("logout")
    public Result<Boolean> logout() {
        return authService.logout() ? Result.success() : Result.failure();
    }

    @Operation(summary = "获取验证码", description = "获取图形验证码")
    @ApiResponse(responseCode = "200", description = "获取成功")
    @GetMapping("captcha")
    public Result<String> captcha(@RequestParam("captchaKey") String captchaKey) {
        return Result.success(authService.captcha(captchaKey));
    }
}
